package cn.tedu;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Demo03 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = sc.nextLine();
        System.out.println("请输入密码");
        String password = sc.nextLine();
        try (Connection conn = DBUtils.getConn()){
            String sql = "SELECT password FROM user WHERE username=?";
            //创建预编译的SQL执行对象
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,username);//替换?
            ResultSet rs = ps.executeQuery();//执行查询
            //判断有没有查询到数据
            if (rs.next()){//查询到了
                String pw = rs.getString(1);//取出查询到的密码
                //判断用户输入的和查询到的是否相等
                if (pw.equals(password)){
                    System.out.println("登录成功!");
                }else System.out.println("密码错误!");
            }else{//用户名不存在
                System.out.println("用户名不存在");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }
}
